package com.zide.file.service.impl;

import com.zide.common.constant.HttpStatus;
import com.zide.common.exception.ServiceException;
import com.zide.common.utils.file.FileUploadUtils;
import com.zide.common.utils.file.FileUtils;
import com.zide.file.config.LocalFileConfig;
import com.zide.file.domain.vo.result.FileUploadResultVO;
import com.zide.file.service.IImageFileService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 石添
 * @date 2024/4/29
 */
@Service
public class ImageFileService implements IImageFileService {

    @Override
    public FileUploadResultVO uploadAvatar(MultipartFile file) {
        try {
            // 上传文件路径
            String filePath = LocalFileConfig.getUploadAvatarPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = LocalFileConfig.getAvatarPath() + fileName;
            FileUploadResultVO fileResultVO = new FileUploadResultVO();
            fileResultVO.setUrl(url);
            fileResultVO.setFilename(FileUtils.getName(fileName));
            return fileResultVO;
        } catch (Exception e) {
            throw new ServiceException(HttpStatus.ERROR,e.getMessage());
        }
    }
}
